//
//  SSCollectionDataTool.m
//  Travel_World
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "SSCollectionDataTool.h"

@interface SSCollectionDataTool ()
@property (nonatomic, assign)ModelType modeltype;
@end

@implementation SSCollectionDataTool

- (void)createModel:(ModelType)model
{
    self.modeltype = model;
}

+ (SSCollectionDataTool *)shareSSCollectionDataToolWithModelType:(ModelType)model
{
    static SSCollectionDataTool *tool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        tool = [[SSCollectionDataTool alloc] init];

        [tool openDB];
        [tool createTabelWithModelType:model];
    });
    
    return tool;
}

- (void)openDB
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];

    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Travel_World.sqlite"];
    
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    NSLog(@"%@", documentPath);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库已经打开");
    } else {
        NSLog(@"数据库打开失败:%d", result);
    }
}

- (void)createTabelWithModelType:(ModelType)modelType
{
    self.modeltype = modelType;
    NSString *sqlStr = nil;
   
    if (self.modeltype == DiscountModel) {
        sqlStr = @"create table if not exists TravelWorld(number integer primary key autoincrement, title text, photo text, price text, url text)";
    } else if (self.modeltype == CountryModel) {
        sqlStr = @"create table if not exists Country(number integer primary key autoincrement, uId text , cnname text ,enname text , photo text)";
    } else if (self.modeltype == CityModel) {
        sqlStr = @"create table if not exists City(number integer primary key autoincrement, uId text , cnname text ,enname text , photo text)";
    }
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK ) {
        NSLog(@"表创建成功");
    } else {
        NSLog(@"表创建失败:%d", result);
    }
}
- (SelectInTable)isIntable:(id)model
{
    NSString *sqlStr = nil;
    if (self.modeltype == DiscountModel) {
        
        SSCollectionModel *disModel = model;
        sqlStr = [NSString stringWithFormat:@"select title from TravelWorld where title = '%@'", disModel.title];
    } else if (self.modeltype == CountryModel) {
        
        TLS_countryModel *couModel = model;
        sqlStr = [NSString stringWithFormat:@"select cnname from Country where cnname = '%@'", couModel.cnname];
    } else if (self.modeltype == CityModel) {
        
        TLS_cityModel *citModel = model;
        sqlStr = [NSString stringWithFormat:@"select cnname from City where cnname = '%@'", citModel.cnname];
    }

    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            sqlite3_finalize(stmt);
            
            return InTable;
        } else {
            
            sqlite3_finalize(stmt);
            
            return NotInTable;
        }
    } else {
        
        NSLog(@"查询失败:%d", result);
    }
    
    sqlite3_finalize(stmt);

    return SelectError;
}

- (void)inserCollectionData:(id)model
{
    // 折扣
    NSString *sqlStr = nil;
    if (self.modeltype == DiscountModel) {
        
        SSCollectionModel *disModel = model;
        sqlStr = [NSString stringWithFormat:@"insert into TravelWorld(title, photo, price, url) values('%@', '%@', '%@', '%@')", disModel.title, disModel.pic, disModel.price, disModel.url];
    } else if (self.modeltype == CountryModel) {
        
        TLS_countryModel *couModel = model;
        sqlStr = [NSString stringWithFormat:@"insert into Country(cnname, enname, photo, uId) values('%@', '%@', '%@', '%@')",couModel.cnname, couModel.enname, couModel.photo, couModel.Id];
    } else if (self.modeltype == CityModel) {
        
        TLS_cityModel *citModel = model;
        sqlStr = [NSString stringWithFormat:@"insert into City(cnname, enname, photo, uId) values('%@', '%@', '%@', '%@')",citModel.cnname, citModel.enname, citModel.photo, citModel.Id];
    }
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"收藏成功");
    } else {
        NSLog(@"收藏失败:%d", result);
    }
}

- (void)deleteCollectoinData:(id)model modelType:(ModelType)modelType
{
    // 折扣
    NSString *sqlStr = nil;
    if (modelType == DiscountModel) {
        SSCollectionModel *disModel = model;
        sqlStr = [NSString stringWithFormat:@"delete from TravelWorld where title = '%@'", disModel.title];
    } else if (modelType == CountryModel) {
        TLS_countryModel *couModel = model;
        sqlStr = [NSString stringWithFormat:@"delete from Country where cnname = '%@'",couModel.cnname];
    } else if (modelType == CityModel) {
        TLS_cityModel *citModel = model;
        sqlStr = [NSString stringWithFormat:@"delete from City where cnname = '%@'",citModel.cnname];
    }
    
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        
        NSLog(@"取消成功");
    } else {
        NSLog(@"取消失败:%d", result);
    }
}


- (NSMutableArray *)selectAllCollectionDataModelType:(ModelType)modelType
{
    NSMutableArray *collectionArr = [NSMutableArray array];
    
    if (modelType == DiscountModel)
    {
        NSString *sqlStr = @"select * from TravelWorld";
        sqlite3_stmt *stmt = nil;
        
        int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
        
        if (result == SQLITE_OK) {
            NSLog(@"查询成功");
            
            while (sqlite3_step(stmt) == SQLITE_ROW) {
                const unsigned char *title = sqlite3_column_text(stmt, 1);
                const unsigned char *photo = sqlite3_column_text(stmt, 2);
                const unsigned char *price = sqlite3_column_text(stmt, 3);
                const unsigned char *url = sqlite3_column_text(stmt, 4);
                
                NSString *travelTitle = [NSString stringWithUTF8String:(const char *) title];
                NSString *travelPhoto = [NSString stringWithUTF8String:(const char *) photo];
                NSString *travelPrice = [NSString stringWithUTF8String:(const char *) price];
                NSString *travelUrl = [NSString stringWithUTF8String:(const char *) url];
                
                SSCollectionModel *collectionModel = [[SSCollectionModel alloc] init];
                collectionModel.title = travelTitle;
                collectionModel.pic = travelPhoto;
                collectionModel.price = travelPrice;
                collectionModel.url = travelUrl;
                
                [collectionArr addObject:collectionModel];
                
            }
            
        } else {
            NSLog(@"查询失败%d", result);
        }
        
        sqlite3_finalize(stmt);
    }
    
    else if (modelType == CountryModel) {
        NSString *sqlStrCountry = @"select *from Country";
        sqlite3_stmt *stmtCountry = nil;
        int resultCountry = sqlite3_prepare_v2(dbPoint, [sqlStrCountry UTF8String], -1, &stmtCountry, nil);
    
        if (resultCountry == SQLITE_OK) {
            NSLog(@"查询国家成功");
            
            while (sqlite3_step(stmtCountry) == SQLITE_ROW) {
                
                const unsigned char *Id = sqlite3_column_text(stmtCountry, 1);
                const unsigned char *cnname = sqlite3_column_text(stmtCountry, 2);
                const unsigned char *enname = sqlite3_column_text(stmtCountry, 3);
                const unsigned char *photo = sqlite3_column_text(stmtCountry, 4);
                
                
                NSString *countryId = [NSString stringWithUTF8String:(const char *) Id];
                NSString *countryCnname = [NSString stringWithUTF8String:(const char *) cnname];
                NSString *countryEnname = [NSString stringWithUTF8String:(const char *) enname];
                NSString *countryPhoto = [NSString stringWithUTF8String:(const char *) photo];
                
                
                TLS_countryModel *couModel = [[TLS_countryModel alloc] init];
                couModel.cnname = countryCnname;
                couModel.enname = countryEnname;
                couModel.photo = countryPhoto;
                couModel.Id = countryId;
                
                [collectionArr addObject:couModel];
                
            }
            
        } else {
            NSLog(@"查询国家失败:%d",resultCountry);
        }
        sqlite3_finalize(stmtCountry);
    }
    
    else if (modelType == CityModel) {
    
        NSString *sqlStrCity = @"select *from City";
        sqlite3_stmt *stmtCity = nil;
        
        int resultCity = sqlite3_prepare_v2(dbPoint, [sqlStrCity UTF8String], -1, &stmtCity, nil);
        
        if (resultCity == SQLITE_OK) {
            NSLog(@"查询城市成功");
            
            while (sqlite3_step(stmtCity) == SQLITE_ROW) {
                
                const unsigned char *Id = sqlite3_column_text(stmtCity, 1);
                const unsigned char *cnname = sqlite3_column_text(stmtCity, 2);
                const unsigned char *enname = sqlite3_column_text(stmtCity, 3);
                const unsigned char *photo = sqlite3_column_text(stmtCity, 4);
                
                NSString *cityId = [NSString stringWithUTF8String:(const char *) Id];
                NSString *cityCnname = [NSString stringWithUTF8String:(const char *) cnname];
                NSString *cityEnname = [NSString stringWithUTF8String:(const char *) enname];
                NSString *cityPhoto = [NSString stringWithUTF8String:(const char *) photo];
                
                TLS_cityModel *citModel = [[TLS_cityModel alloc] init];
                citModel.cnname = cityCnname;
                citModel.enname = cityEnname;
                citModel.photo = cityPhoto;
                citModel.Id = cityId;
                
                [collectionArr addObject:citModel];
                
            }
            
        } else {
            NSLog(@"查询城市失败:%d",resultCity);
        }
        sqlite3_finalize(stmtCity);
    }
 
    return collectionArr;
}

@end
